//done
package Server;

import java.sql.Date;

import Entities.Product;
import Server.SQL_Manager;

/**
 * This class gets a message from Client and converts 
 * it to a function, and returns the result to Client.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil 
 */
public class ConnectionToUser {
	
	private String[] clientmessage = null;
	private Object messagetoclient = null;
	
	/**
	 * Default Constructor
	 */
	public ConnectionToUser(String[] clientmessage) {
		super();
		this.clientmessage = clientmessage;
	}

	/**
	 * this fuction is getter of clientmessage
	 * @return the clientmessage
	 */
	public String[] getClientmessage() {
		return clientmessage;
	}
	
	/**
	 * This fuction this fuction is to search for all products 
	 */
	private void getProductDetails()
	{
		try
		{
			SQL_Manager sql_Manager = new SQL_Manager();
			String[] productdetails = sql_Manager.searchForProduct(Long.parseLong(clientmessage[1]));
			if (productdetails != null)
			{
				messagetoclient = productdetails;			
			}
		}catch(Exception e)	{};		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if product details successfully inserted,
	 * else to "IlegalCataloguenumber" 
	 */
	private void insertProductDetails()
	{
		Product product = new Product();
		try
		{
			product.setPrice(Double.parseDouble(clientmessage[1]));
			product.setName(clientmessage[2]);
			product.setDescription(clientmessage[3]);
			product.setProdID(Long.parseLong(clientmessage[4]));
			product.setProdPic(clientmessage[5]);
			if (product.insertToDataBase())
			{
				messagetoclient = "OK";
			}
			else
				messagetoclient = "IlegalCataloguenumber";
		}catch(Exception e){};	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if product details successfully updated,
	 * else to "IlegalCataloguenumber" 
	 */
	private void updateProductDetails()
	{
		Product product = new Product();
		try
		{
			product.setPrice(Double.parseDouble(clientmessage[1]));
			product.setName(clientmessage[2]);
			product.setDescription(clientmessage[3]);
			product.setProdID(Long.parseLong(clientmessage[4]));
			product.setProdPic(clientmessage[5]);
			if (product.ServerUpdateProduct())
			{
				messagetoclient = "OK";
			}
			else
				messagetoclient = "IlegalCataloguenumber";
		}catch(Exception e){};		
	}
	
	
	/**
	 * This fuction update (messagetoclient) to "OK" if product successfully removed 
	 * from data base, else to "ERROR" 
	 */
	private void removeProduct()
	{
		try
		{
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.RemoveProduct(Long.parseLong(clientmessage[1]));
		}
		catch(NumberFormatException e)	
		{
			messagetoclient = "ERROR";
		};	
	}
	
	/**
	 * This fuction update (messagetoclient) to Long array of all products ID  
	 * or to null on error 
	 */
	private void loadAllProductsID()
	{
		try
		{
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllProductID();
		}catch (Exception except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to array of String of all catalog sections
	 * authorizations details or to null on error. 
	 */
	private void loadAllProductsNames()
	{
		try
		{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllProductsNames();	
		}catch (Exception except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if template successfully added  
	 * to data base, or to "IlegalID" on SQL error, else to null. 
	 */
	private void createTemplate(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			String name, sector, message;
			long pID, cID;
			cID = Long.parseLong(clientmessage[1]);
			name = clientmessage[2];
			sector = clientmessage[3];
			message = clientmessage[4];
			pID = Long.parseLong(clientmessage[5]);
			messagetoclient = sql_Manager.createTemplate(cID,name,sector,message,pID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) array of long of all customersID  
	 * or to null on error 
	 */
	private void loadAllCustomers(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllCustomers();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of customers details
	 * or to null on error 
	 */
	private void loadCustomer(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadCustomer(Long.parseLong(clientmessage[1]));
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of Address Zone details
	 * or to null on error 
	 */
	private void loadAllAddressZones(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllAreas();			
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if customer successfully added  
	 * to data base, or to "IlegalID" on SQL error, else to null. 
	 */
	private void addCustomer(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			long cid = Long.parseLong(clientmessage[1]);
			String name = clientmessage[2];
			int age = Integer.parseInt(clientmessage[3]);
			int area = Integer.parseInt(clientmessage[4]);
			String phone = clientmessage[5];
			if (sql_Manager.addCustomer(cid, name, age, area,phone,1))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if customer info successfully update  
	 * in data base, else to null. 
	 */
	private void updateCustomer(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			long cid = Long.parseLong(clientmessage[1]);
			String name = clientmessage[2];
			int age = Integer.parseInt(clientmessage[3]);
			int area = Integer.parseInt(clientmessage[4]);
			String phone = clientmessage[5];
			if (sql_Manager.updateCustomer(cid, name, age, area,phone))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of templates
	 * or to null on error 
	 */
	private void loadAllTemplates(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllTemplates();			
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of templates details
	 * or to null on error 
	 */
	private void loadTemplate(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadTemplate(Long.parseLong(clientmessage[1]));
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
		
	/**
	 * This fuction update (messagetoclient) to "OK" if template successfully updated  
	 * in data base, or to null on SQL error. 
	 */
	private void updateTemplate(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			String name, sector, message;
			long pID, cID;
			cID = Long.parseLong(clientmessage[1]);
			name = clientmessage[2];
			sector = clientmessage[3];
			message = clientmessage[4];
			pID = Long.parseLong(clientmessage[5]);
			messagetoclient = sql_Manager.updateTemplate(cID,name,sector,message,pID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of all categories of product prodID details
	 * or to null on error 
	 */
	public void loadAllCategoriesOfProduct(){
		try{	
			long prodID = Long.parseLong(clientmessage[1]);
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllCategoriesOfProduct(prodID);			
		}
		catch (Exception except){
			messagetoclient = null;
		}
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of all categories details
	 * or to null on error 
	 */
	private void loadAllCategories(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllCategories();			
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of all products in the category categoryID  
	 * details or to null on error. 
	 */
	private void loadCategoryProducts(){
		try{	
			int categoryID = Integer.parseInt(clientmessage[1]);
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadCategoryProducts(categoryID);	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the Address Zone successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void AddAddressZone(){
		int AddressZoneID = Integer.parseInt(clientmessage[1]);
		String areaName = clientmessage[2];
		SQL_Manager sql_Manager = null;
		try{	
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		
		if (sql_Manager.addAddressZone(AddressZoneID, areaName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the Address Zone successfully
	 *  updated on data base, or to null on SQL error.  
	 */
	private void UpdateAddressZone(){
		int addressZoneID = Integer.parseInt(clientmessage[1]);
		String areaName = clientmessage[2];
		SQL_Manager sql_Manager = null;
		try{	
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		
		if(sql_Manager.updateAddressZone(addressZoneID, areaName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the category successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void addCategory(){
		int categoryID=0;
		SQL_Manager sql_Manager = null;
		try{	
			categoryID = Integer.parseInt(clientmessage[1]);
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String sectionName = clientmessage[2];		
		if (sql_Manager.addCategory(categoryID, sectionName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the category successfully
	 *  updated in data base, or to null on SQL error.  
	 */
	private void updateCategory(){
		int categoryID=0;
		SQL_Manager sql_Manager = null;
		try{	
			categoryID = Integer.parseInt(clientmessage[1]);
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String sectionName = clientmessage[2];		
		if (sql_Manager.updateCategory(categoryID, sectionName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the category successfully
	 *  removed from data base, or to null on SQL error.  
	 */
	private void removeCategory(){
		int categoryID=0;
		SQL_Manager sql_Manager = null;
		try{	
			categoryID = Integer.parseInt(clientmessage[1]);
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		if (sql_Manager.removeCategory(categoryID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the type successfully
	 *  removed from data base, or to null on SQL error.  
	 */
	private void removeCatalogueType(){
		int typeID=0;
		SQL_Manager sql_Manager = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		if (sql_Manager.removeType(typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the type successfully
	 *  updated in data base, or to null on SQL error.  
	 */
	private void updateCatalogueType(){
		int typeID=0;
		SQL_Manager sql_Manager = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String typeName = clientmessage[2];		
		if (sql_Manager.updateType(typeID, typeName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the type successfully
	 *  added to data base, or to null on SQL error.  
	 */
	private void addCataloguetype(){
		int typeID=0;
		SQL_Manager sql_Manager = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		String typeName = clientmessage[2];		
		if (sql_Manager.addtype(typeID, typeName))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of all type products 
	 * details or to null on error. 
	 */
	private void loadTypeProduct(){
		try{	
			int tID = Integer.parseInt(clientmessage[1]);
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadTypeProduct(tID);	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the product successfully 
	 * removed from the type, or to null on SQL error.  
	 */
	private void removeProductFromType(){
		int typeID=0;
		long productID=0;
		SQL_Manager sql_Manager = null;
		try{	
			typeID = Integer.parseInt(clientmessage[1]);
			productID = Long.parseLong(clientmessage[2]);
			sql_Manager = new SQL_Manager();			
		}
		catch (Exception except){
			messagetoclient = null;
			return;
		}
		if (sql_Manager.removeProductFromType(typeID, productID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the product successfully 
	 * added to the type, or to null on SQL error.  
	 */
	private void addProductToType()
	{
		int typeID=0;
		long productID=0;
		SQL_Manager sql_Manager = null;
		try
		{	
			typeID = Integer.parseInt(clientmessage[1]);
			productID = Long.parseLong(clientmessage[2]);
			sql_Manager = new SQL_Manager();			
		}catch (Exception except)
		{
			messagetoclient = null;
			return;
		}
		if (sql_Manager.addProductToType(typeID, productID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of all category types 
	 * details or to null on error. 
	 */
	private void loadAllCategoryTypes()
	{
		try
		{	
			int categoryID = Integer.parseInt(clientmessage[1]);
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllCategoryTypes(categoryID);	
		}catch (Exception except)
		{
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to array of String of all the types
	 * details or to null on error. 
	 */
	private void loadAllTypes(){
		try{	
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllTypes();	
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the type successfully 
	 * removed from the category, or to null on SQL error.  
	 */
	private void removeTypeFromCategory()
	{
		int typeID=0;
		int categoryID=0;
		SQL_Manager sql_Manager = null;
		try
		{	
			categoryID = Integer.parseInt(clientmessage[1]);
			typeID = Integer.parseInt(clientmessage[2]);
			sql_Manager = new SQL_Manager();			
		}catch (Exception except)
		{
			messagetoclient = null;
			return;
		}
		if (sql_Manager.removeTypeFromCategory(categoryID, typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if the type successfully 
	 * added to the category, or to null on SQL error.  
	 */
	private void addTypeToCategory()
	{
		int typeID=0;
		int categoryID=0;
		SQL_Manager sql_Manager = null;
		try
		{	
			categoryID = Integer.parseInt(clientmessage[1]);
			typeID = Integer.parseInt(clientmessage[2]);
			sql_Manager = new SQL_Manager();			
		}catch (Exception except)
		{
			messagetoclient = null;
			return;
		}
		if (sql_Manager.addTypeToCategory(categoryID, typeID))
			messagetoclient = "OK";
		else
			messagetoclient = "ERROR";		
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if Deal Sale successfully updated  
	 * in data base, or to null on SQL error. 
	 */
	private void updateDealSale(){
		long dealSaleID;
		String status, year, month, day, tmp;
		try{
			SQL_Manager templateUpdate = new SQL_Manager();
			dealSaleID = Long.parseLong(clientmessage[1]);
			tmp = clientmessage[2];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date startdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date endate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			status = clientmessage[4];			
			messagetoclient = templateUpdate.updateDealSale(dealSaleID, startdate, endate, status );
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if Deal Sale successfully 
	 * inserted to data base, or to null on SQL error. 
	 */
	private void saveDealSale(){
		long dealSaleID, campaignID;
		String status, tmp, year, month, day;
		try{
        	SQL_Manager templateUpdate = new SQL_Manager();
			dealSaleID = Long.parseLong(clientmessage[1]);
			tmp = clientmessage[2];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date startdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date endate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
	        status = clientmessage[4];
	        campaignID =  Long.parseLong(clientmessage[5]);
			messagetoclient = templateUpdate.saveDealSale(dealSaleID, startdate, endate, status, campaignID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to array of Object of Deal Sale details (as Object[]) 
	 */
	private void loadDealSale(){
		long dealSaleID;
		try{
			SQL_Manager templateUpdate = new SQL_Manager();
			dealSaleID = Long.parseLong(clientmessage[1]);
			messagetoclient = templateUpdate.loadDealSale(dealSaleID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to Deal Sale ID as Vector<Long> 
	 */
	private void loadAllDealSales(){
		try{
			SQL_Manager templateUpdate = new SQL_Manager();
			messagetoclient = templateUpdate.loadAllDealSales();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This function loads all customer(CustID) interests as Vector<Long> 
	 */
	private void LoadAllCustomerInterests(){
		try{
			SQL_Manager customersUpdate = new SQL_Manager();
			long CustID = Long.parseLong(clientmessage[1]);
			messagetoclient = customersUpdate.loadAllCustomerInterests(CustID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This function adds a new customer interest to DB 
	 */
	private void AddCustomerInterest(){
		try{
			SQL_Manager customersUpdate = new SQL_Manager();
			long CustID = Long.parseLong(clientmessage[1]);
			int CatID = Integer.parseInt(clientmessage[2]);
			int Rate = Integer.parseInt(clientmessage[3]);
			messagetoclient = customersUpdate.addCustomerInterest(CustID, CatID, Rate);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This function deletes a customer interest from DB 
	 */
	private void DeleteCustomerInterest(){
		try{
			SQL_Manager customersUpdate = new SQL_Manager();
			long CustID = Long.parseLong(clientmessage[1]);
			int CatID = Integer.parseInt(clientmessage[2]);
			messagetoclient = customersUpdate.deleteCustomerInterest(CustID, CatID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to customers IDs as Vector<Long> 
	 */
	private void loadAllCustomersAtMarketSegment(){
		try{
			SQL_Manager customersUpdate = new SQL_Manager();
			int sector = Integer.parseInt(clientmessage[1]);
			messagetoclient = customersUpdate.loadAllCustomersAtMarketSegment(sector);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to customers IDs for sales man as Vector<Long> 
	 */
	private void loadAllCustomersForSalesMan(){
		try{
			SQL_Manager sellsMenUpdate = new SQL_Manager();
			messagetoclient = sellsMenUpdate.loadAllCustomersForSalesMan(clientmessage[1]);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to Sales men IDs as Vector<Long> 
	 */
	private void loadAllSalesMen(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
 			messagetoclient = sql_Manager.loadAllSalesMen();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if Sales men details updated successfully to salesmanschedule, or to "ERROR" on MysQl error  
	 */
	private void updateSalesManSchedule(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			String schedule = clientmessage[1];
			long activeID = Long.parseLong(clientmessage[2]);
 			if (sql_Manager.updateSalesManSchedule(schedule, activeID))
 				messagetoclient = "OK";
 			else
 				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to Deal Sales IDs as long[]
	 */
	private void loadDealSaleForCustomer(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			long cID = Long.parseLong(clientmessage[1]);
			messagetoclient = sql_Manager.loadDealSaleForCustomer(cID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if successfully inserted rejected for Deal Sale and deleted the current customer from the current sales man schedule, else update (messagetoclient) to "ERORR"
	 */
	private void rejectedDealSale(){
		String tmp, year, month, day;
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			long cID = Long.parseLong(clientmessage[1]);
			long active = Long.parseLong(clientmessage[2]);
			String message = clientmessage[3];
			tmp = clientmessage[4];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date date = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			if (sql_Manager.rejectedDealSale(cID,active ,message, date))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
		
	/**
	 * This fuction update (messagetoclient) to "OK" if successfully inserted  purchase for Deal Sale, else "ERROR" 
	 */
	private void purchasedDealSale(){
		String tmp, year, month, day;
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			long cID = Long.parseLong(clientmessage[1]);
			long active = Long.parseLong(clientmessage[2]);
			tmp = clientmessage[3];
			year = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
			month = tmp.substring(0,tmp.indexOf("/"));
			tmp = tmp.substring(tmp.indexOf("/")+1, tmp.length());
	        day = tmp;
	        Date date = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			if (sql_Manager.purchasedDealSale(cID,active,date))
				messagetoclient = "OK";
			else
				messagetoclient = "ERROR";
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to "OK" if successfully the data analyzed and client importance updated, else "ERROR"
	 */
	private void ActivityTracking(){
		try{
			SQL_Manager customersUpdate = new SQL_Manager();
			messagetoclient = customersUpdate.activityTracking();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String of response report 
	 */
	private void getResponseReport(){
		try{
			long activeID = Long.parseLong(clientmessage[1]);
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.getResponseReport(activeID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String of customer report 
	 */
	private void customerReport(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.customerReport();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to String array of all market segments ID  
	 * or to null on error 
	 */
	private void loadAllMarketSegments(){
		try{
			SQL_Manager sql_Manager = new SQL_Manager();
			messagetoclient = sql_Manager.loadAllMarketSegments();
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction update (messagetoclient) to all interested customers IDs as Vector<Long> 
	 */
	private void loadAllInterestedCustomers(){
		try{
			SQL_Manager customersUpdate = new SQL_Manager();
			long prodID = Integer.parseInt(clientmessage[1]);
			messagetoclient = customersUpdate.loadAllInterestedCustomers(prodID);
		}
		catch (Exception except){
			messagetoclient = null;
		}	
	}
	
	/**
	 * This fuction return (messagetoclient): message that server send to client
	 */
	public synchronized Object sendMessageToClient(){
		switch(clientmessage[0]){
		case "LoadProduct":
			getProductDetails();
			return messagetoclient;
		 	
		case "AddProduct":
			insertProductDetails();
			return messagetoclient; 
			
		case "UpdateProduct":
			updateProductDetails();
			return messagetoclient;
			
		case "RemoveProduct":
			removeProduct();
			return messagetoclient;
			
		case "LoadAllProductsID":
			loadAllProductsID();
			return messagetoclient;
			
		case "LoadAllProductsNames":
			loadAllProductsNames();
			return messagetoclient;
			
		case "LoadCustomer":
			loadCustomer();
			return messagetoclient;
			
		case "LoadAllCustomers":
			loadAllCustomers();
			return messagetoclient;
			
		case "AddCustomer":
			addCustomer();
			return messagetoclient;
			
		case "UpdateCustomer":
			updateCustomer();
			return messagetoclient;
			
		case "LoadTemplate":
			loadTemplate();
			return messagetoclient;
			
		case "LoadAllTemplates":
			loadAllTemplates();
			return messagetoclient;
			
		case "CreateTemplate":
			createTemplate();
			return messagetoclient;
			
		case "UpdateTemplate":
			updateTemplate();
			return messagetoclient;
			
		case "loadAllAddressZones":
			loadAllAddressZones();
			return messagetoclient;
			
		case "AddAddressZone":
			AddAddressZone();
			return messagetoclient;
			
		case "UpdateAddressZone":
			UpdateAddressZone();
			return messagetoclient;
			
		case "LoadDealSale":
			loadDealSale();
			return messagetoclient;
			
		case "LoadAllDealSales":
			loadAllDealSales();
			return messagetoclient;
			
		case "SaveDealSale":
			saveDealSale();
			return messagetoclient;
			
		case "UpdateDealSale":
			updateDealSale();
			return messagetoclient;
			
		case "LoadAllCustomerInterests":
			LoadAllCustomerInterests();
			return messagetoclient;
			
		case "AddCustomerInterest":
			AddCustomerInterest();
			return messagetoclient;
			
		case "DeleteCustomerInterest":
			DeleteCustomerInterest();
			return messagetoclient;
			
		case "LoadAllCategoriesOfProduct":
			loadAllCategoriesOfProduct();
			return messagetoclient;
			
		case "LoadAllCategories":
			loadAllCategories();
			return messagetoclient;
			
		case "LoadCategoryProducts":
			loadCategoryProducts();
			return messagetoclient;
			
		case "AddCategory":
			addCategory();
			return messagetoclient;
		
		case "UpdateCategory":
			updateCategory();
			return messagetoclient;
			
		case "RemoveCategory":
			removeCategory();
			return messagetoclient;
			
		case "AddType":
			addCataloguetype();
			return messagetoclient;
			
		case "UpdateType":
			updateCatalogueType();
			return messagetoclient;
			
		case "RemoveType":
			removeCatalogueType();
			return messagetoclient;
			
		case "LoadTypeProduct":
			loadTypeProduct();
			return messagetoclient;
			
		case "AddProductToType":
			addProductToType();
			return messagetoclient;
			
		case "RemoveProductFromType":
			removeProductFromType();
			return messagetoclient;
			
		case "AddTypeToCategory":
			addTypeToCategory();
			return messagetoclient;
			
		case "RemoveTypeFromCategory":
			removeTypeFromCategory();
			return messagetoclient;
			
		case "LoadAllCategoryTypes":
			loadAllCategoryTypes();
			return messagetoclient;
			
		case "LoadAllTypes":
			loadAllTypes();
			return messagetoclient;
			
		case "LoadAllSalesMen":
			loadAllSalesMen();
			return messagetoclient;
			
		case "LoadAllCustomersForSalesMan":
			loadAllCustomersForSalesMan();
			return messagetoclient;
			
		case "UpdateSalesManSchedule":
			updateSalesManSchedule();
			return messagetoclient;
			
		case "loadDealSaleForCustomer":
			loadDealSaleForCustomer();
			return messagetoclient;
			
		case "rejectedDealSale":
			rejectedDealSale();
			return messagetoclient;
			
		case "purchasedDealSale":
			purchasedDealSale();
			return messagetoclient;
			
		case "getResponseReport":
			getResponseReport();
			return messagetoclient;
			
		case "customerReport":
			customerReport();
			return messagetoclient;
		
		case "activityTracking":
			ActivityTracking();
			return messagetoclient;
			
		case "LoadAllMarketSegments":
			loadAllMarketSegments();
			return messagetoclient;
			
		case "LoadAllCustomersAtMarketSegment":
			loadAllCustomersAtMarketSegment();
			return messagetoclient;
			
		case "LoadAllInterestedCustomers":
			loadAllInterestedCustomers();
			return messagetoclient;
			
		default: 
			return null;
		}							
	}
}
